home *** CD-ROM | disk | FTP | other *** search
- /***
- * logIO.c
- *
- * IO routines to the log file
- *
- ***/
-
- #include "Headers.h"
-
- #include "PageMonRes.h"
- #include "logIO.h"
- #include "Folders.h"
- #include <Errors.h>
- #include "Exceptions.h"
-
- #define NEW_LINE 0xD // New line character
-
- /**
- * WriteToLog
- *
- * Write a line out to the log file detailing the number of pages we are about to print.
- * Find this thing in the Pref folder, under Printer Prefs. :)
- *
- **/
-
- OSErr WriteToLog (long numPages)
- {
- OSErr anErr;
-
- FSSpec styleLogFile;
- short refNum;
- long writeSize;
-
- Str255 string;
- Str255 stringLine;
-
- /**
- ** Load in the string resource that contains the path of the file from the
- ** prefs folder to the log file
- **/
-
- string[0] = 0;
- GetIndString (string, PAGE_MON_STRS, PAGE_MON_LOG_PATH);
- anErr = ResError ();
- if (anErr == noErr && string[0] == 0) anErr = -1;
- stringLine[0] = 0;
- if (anErr == noErr) {
- GetIndString (stringLine, PAGE_MON_STRS, PAGE_MON_PG_MESSAGE);
- anErr = ResError ();
- if (anErr == noErr && stringLine[0] == 0) anErr = -1;
- }
- nrequire (anErr, FailedFindResStrs);
-
- /**
- ** Great. Now find out where the style writer II log is located -- get an fsspec for
- ** the thing. Use find folder to do this... Note, we have to setup a a4 world
- ** for a few seconds while we do this cause we have to get access to a couple of strings!
- **/
-
- anErr = FindFolder (kOnSystemDisk, kPreferencesFolderType, false,
- &(styleLogFile.vRefNum),
- &(styleLogFile.parID));
- nrequire (anErr, FailedFindPrefFolder);
-
- if (string[0] > 63) string[0] = 63;
- BlockMove (string, styleLogFile.name, string[0]+1);
-
- /**
- ** We have an fsspec that "should" be valid at this point. Next, we
- ** just have to see if the file exists. If so, we can create it...
- **/
-
- anErr = FSpCreate (&styleLogFile, 'ttxt', 'TEXT', smSystemScript);
- require (anErr == noErr || anErr == dupFNErr, FailedFileCreate);
-
- /**
- ** Now we also have a created file. Next job is to open it for writing
- **/
-
- anErr = FSpOpenDF (&styleLogFile, fsWrPerm, &refNum);
- nrequire (anErr, FailedOpenFile);
- anErr = SetFPos (refNum, fsFromLEOF, 0);
- nrequire (anErr, FailedMoveEOF);
-
- /**
- ** Build the string that we are going to stuff into the file
- **/
-
- NumToString (numPages, string);
- BlockMove (&(string[1]), &(stringLine[stringLine[0]+1]), string[0]);
- stringLine[0] += string[0];
-
- stringLine[0] += 1; // Add a newline!
- stringLine[stringLine[0]] = NEW_LINE;
-
- /**
- ** Write that puppy out
- **/
-
- writeSize = stringLine[0];
- anErr = FSWrite (refNum, &writeSize, &(stringLine[1]));
- nrequire (anErr, FailedWrite);
-
- /**
- ** If we drop to here, we have no error to deal with... :)
- **/
-
- FSClose (refNum);
- return noErr;
-
- /**
- ** Handle a small :) number of errors
- **/
-
- FailedWrite: // Failed to do the write
- FailedMoveEOF: // Couldn't move to end of file!
- FSClose (refNum);
- FailedOpenFile: // Didn't open log file!
- FailedFileCreate: // We could not create the file for some reason!
- FailedFindPrefFolder: // No prefs folder on this machine
- FailedFindResStrs: // Didn't get all the strings back!
- anErr = noErr;
- }